<template>
  <!-- 一条文章单元格 -->
  <van-cell>
    <!-- 标题区域的插槽 -->
    <template #title>
    <div class="title-box">
    <!-- 标题 -->
    <span>{{ articlesObj.title }}</span>
    <!-- 单图 -->
    <img class="thumb" :src="articlesObj.cover.images[0]" v-if="articlesObj.cover.type===1" />
        </div>
      <!-- 三张图片 -->
      <div class="thumb-box"  v-if="articlesObj.cover.type>1">
        <img class="thumb" v-for="(item,index) in articlesObj.cover.images" :key="index" :src="item"/>
      </div>
      </template>
          <!-- label 区域的插槽 -->
    <template #label>
      <div class="label-box">
        <div>
          <span>{{articlesObj.aut_name}}</span>
          <span>{{articlesObj.comn_count}}</span>
          <span>{{articlesObj.pubdate}}</span>
        </div>
        <!-- 反馈按钮 -->
        <van-icon name="cross" @click="closeIcon" />
      </div>
      <div>
          <!-- 弹出层的提示区 -->
        <van-action-sheet v-model="show" :actions="actions" @select="onSelect" :cancel-text="cancelText" get-container="body" close-on-click-action @cancel="onCancel"/>
      </div>
    </template>
  </van-cell>
</template>

<script>
// 引入反馈弹出层
import { firstActions, secondActions } from '@/api/reports.js'
import { dislikeAticleAPI, articleReportsAPI } from '@/api'
export default {
  name: 'ArticleItem',
  props: {
    articlesObj: {
      type: Object,
      default: () => {}
    }
  },
  data () {
    return {
      show: false,
      actions: firstActions,
      cancelText: '取消'
    }
  },
  methods: {
    // 关闭按钮
    closeIcon () {
      this.show = true
    },
    // 一级弹出层
    async onSelect (action) {
      // action绑定的数据对象, index是索引
      if (action.name === '反馈垃圾内容') {
        // 证明点击的是反馈垃圾内容
        this.actions = secondActions
        this.show = true
        this.cancelText = '返回'
      } else if (action.name === '不感兴趣') {
        try {
          await dislikeAticleAPI({ target: this.articlesObj.art_id })
          this.$Notify({ type: 'success', message: '反馈成功' })
        } catch (error) {
          this.$Notify({ type: 'warning', message: '反馈失败' })
        }
        this.$bus.$emit('refreshPage')
        this.show = false
      } else {
        try {
          await articleReportsAPI({
            target: this.articlesObj.art_id,
            type: action.value,
            remark: '就是不喜欢，就是不爱了'
          })
          this.$Notify({ type: 'success', message: '反馈成功' })
        } catch (error) {
          this.$Notify({ type: 'warning', message: '反馈失败' })
        }
        this.show = false
        this.actions = firstActions
      }
    },
    // 二级弹出层取消事件
    onCancel () {
      if (this.cancelText === '取消') {
        this.show = false
      } else {
        this.actions = firstActions
        this.show = true
        this.cancelText = '取消'
      }
    }

  }
}

</script>

<style scoped lang="less">
/* 标题样式 */
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  text-align: left;
}

/* label描述样式 */
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 文章信息span */
.label-box span{
    margin: 0 3px;
    &:first-child{
        margin-left: 0;
    }
}
/* 图片的样式, 矩形黄金比例：0.618 */
.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

/* 三图, 图片容器 */
.thumb-box {
  display: flex;
  justify-content: space-between;
}
</style>
